<?php

namespace app\admin\controller;

use app\admin\model\User;
use app\common\controller\Backend;

/**
 * 推送报告对比分析
 *
 * @icon fa fa-circle-o
 */
class UserAdminBMessage extends Backend
{
    
    /**
     * UserAdminBMessage模型对象
     * @var \app\admin\model\UserAdminBMessage
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\UserAdminBMessage;
        $this->view->assign("isReadList", $this->model->getIsReadList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            if($this->level == 2) {

                $expertModel = new \app\admin\model\UserExpert();
//                $user_ids = $expertModel->where(['admin_id' => $this->auth->id])->column('user_id');

                $list = $this->model
                    ->with(['user','admin','admins'])
                    ->where($where)
                    ->where('user_admin_b_message.zhuan_id',$this->auth->id)
//                    ->where('user.id','in',$user_ids)
                    ->order($sort, $order)
                    ->paginate($limit);


            }elseif($this->level == 1){
                if (empty($this->user_ids)){
                    $user_ids = [];
                }else{
                    $user_ids = explode(',',$this->user_ids);
                }

                $list = $this->model
                    ->with(['user','admin','admins'])
                    ->where($where)
                    ->where('user.id','in',$user_ids)
                    ->order($sort, $order)
                    ->paginate($limit);
            }else{
                $list = $this->model
                    ->with(['user','admin','admins'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
            }

            foreach ($list as $row) {
                
                $row->getRelation('user')->visible(['nickname']);
				$row->getRelation('admin')->visible(['nickname']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    public function health($ids)
    {
//        $input = input('get.type');
//        if(!empty($input)){
        $model = new \app\admin\model\UserAdminBMessage();
        $message = $model->get($ids);
        $userModel = new User();
        $user = $userModel->get($message['user_id']);
        $this->view->assign('user_info',$user);
        $this->view->assign('type',1);
        $this->view->assign('message_id',$message['topic_id']);
        $this->view->assign('admin_id',$this->auth->id);
        return $this->view->fetch();
//        }else{
//            $model = new User();
//            $user = $model->get($ids);
//            $user['gender'] =  $user['gender'] > 0 ? '男' : '女';
//            $this->view->assign('user_info',$user);
//            $this->view->assign('type',0);
//            $this->view->assign('message_id',0);
//            $this->view->assign('admin_id',$this->auth->id);
//            return $this->view->fetch();
//        }


    }

}
